//
//  NewsCarouselView.m
//  Btpin
//
//  Created by Vic on 2019/3/16.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "NewsCarouselView.h"
#import "LYRollView.h"

@interface NewsCarouselView ()
{
    LYRollView *lineNewsView;
    NSMutableArray *itemDataArray;
}
@end

@implementation NewsCarouselView

- (instancetype)initWithFrame:(CGRect)frame target:(id)target {
    self = [super initWithFrame:frame];
    if (self) {
        itemDataArray = [NSMutableArray arrayWithCapacity:0];
        [self initUI];
        [self loadData];
        self.delegate = target;
    }
    return self;
}
- (void)initUI {
    UIImageView *leftImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(17*Scale, 0, 32*Scale, 34*Scale) imageName:@"news_icon"];
    leftImgView.userInteractionEnabled = NO;
    [self addSubview:leftImgView];
    
//    UIView *view = [UIToolsControl initViewWithFrame:CGRectMake(EndX(leftImgView)+15*Scale, 0, WIDTH-170*Scale, 36*Scale) backgroundColor:THEMECOLOR_OF_RED];
//    [self addSubview:view];
    
    lineNewsView = [[LYRollView alloc] initWithFrame: CGRectMake(EndX(leftImgView)+15*Scale, 0, WIDTH-110*Scale, 36*Scale)];
    lineNewsView.userInteractionEnabled = NO;
//    __weak typeof(self) weakSelf = self;
    lineNewsView.titleArr = @[@"比特聘采用区块链技术打造全球人才第一链区块链企业如何快速、免费获得需要人才？", @"比特聘采用区块链技术打造全球人才第一链区块链企业如何快速、免费获得需要人才？"];
    lineNewsView.block = ^(NSInteger index){
//        [weakSelf rollViewDidClicked:index];
    };
    [self addSubview:lineNewsView];
    
    UIImageView *rightImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-22*Scale, 12*Scale, 7*Scale, 13*Scale) imageName:@"jiantou_right"];
    rightImgView.userInteractionEnabled = NO;
    [self addSubview:rightImgView];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesClick)];
    [self addGestureRecognizer:tapGes];
}
- (void)tapGesClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(newsCarouselViewTap:)]) {
        if (itemDataArray.count) {
            ArticleItemModel *model = itemDataArray[0];
            [self.delegate newsCarouselViewTap:model];
        } else {
            [self.delegate newsCarouselViewTap:nil];
        }
    }
}
- (void)loadData {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"news" forKey:@"channel"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    [BaseNetManager requestWithMethod:POST path:Article_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
            id data = responseObject[@"data"];
            if (data[@"lists"] && [data[@"lists"] isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in data[@"lists"]) {
                    [arr addObject:dic[@"title"]];
                    ArticleItemModel *model = [[ArticleItemModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self->itemDataArray addObject:model];
                }
            }
            if (arr.count) {
                self->lineNewsView.titleArr = arr;
            }
        }
    } failurBlock:^(NSError * _Nonnull error) {
        
    }];
}

@end
